package com.zzyl.nursing.service.impl;

import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzyl.common.core.domain.entity.SysUser;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.SecurityUtils;
import com.zzyl.nursing.dto.AlertDataDto;
import com.zzyl.nursing.dto.AlertDataHandleDto;
import com.zzyl.nursing.dto.AlertDto;
import com.zzyl.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.AlertDataMapper;
import com.zzyl.nursing.domain.AlertData;
import com.zzyl.nursing.service.IAlertDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Arrays;

/**
 * 报警数据Service业务层处理
 *
 * @author ruoyi
 * @date 2025-01-17
 */
@Service
public class AlertDataServiceImpl extends ServiceImpl<AlertDataMapper, AlertData> implements IAlertDataService {
    @Autowired
    private AlertDataMapper alertDataMapper;
    @Autowired
    private ISysUserService iSysUserService;

    /**
     * 查询报警数据
     *
     * @param id 报警数据主键
     * @return 报警数据
     */
    @Override
    public AlertData selectAlertDataById(Long id) {
        return getById(id);
    }

    /**
     * 查询报警数据列表
     *
     * @param alertData 报警数据
     * @return 报警数据
     */
    @Override
    public Page<AlertData> selectAlertDataList(AlertDataDto alertData) {
        //构建Ipage对象
        IPage<AlertData> page = new Page<>(alertData.getPageNum(), alertData.getPageSize());
        //条件查询
        Page<AlertData> dataIPage = (Page<AlertData>) this.lambdaQuery()
                .eq(ObjectUtil.isNotEmpty(alertData.getDeviceName()), AlertData::getDeviceName, alertData.getDeviceName())
                .eq(ObjectUtil.isNotNull(alertData.getStatus()), AlertData::getStatus, alertData.getStatus())
                .between(ObjectUtil.isNotNull(alertData.getStartTime()) && ObjectUtil.isNotNull(alertData.getEndTime()), AlertData::getCreateTime, alertData.getStartTime(), alertData.getEndTime())
                .page(page);
        return dataIPage;
    }

    /**
     * 新增报警数据
     *
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int insertAlertData(AlertData alertData) {
        return save(alertData) ? 1 : 0;
    }

    /**
     * 修改报警数据
     *
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int updateAlertData(AlertDataHandleDto alertData) {

        AlertData data = BeanUtil.toBean(alertData, AlertData.class);
        //判断是否有值
        if (ObjectUtil.isNull(alertData)) {
            throw new BaseException("数据异常");
        }
        //调用SecurityUtils里面的方法
        Long userId = SecurityUtils.getUserId();
        if (ObjectUtil.isNull(userId)) {
            throw new BaseException("没有用户信息");
        }
        //根据id查询userName
        SysUser user = this.iSysUserService.selectUserById(userId);
        if (ObjectUtil.isNull(user)) {
            throw new BaseException("没有该用户");
        }
        data.setProcessorId(userId);
        data.setProcessorName(user.getUserName());
        data.setStatus(1);
        return updateById(data) ? 1 : 0;
    }

    /**
     * 批量删除报警数据
     *
     * @param ids 需要删除的报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataByIds(Long[] ids) {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除报警数据信息
     *
     * @param id 报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataById(Long id) {
        return removeById(id) ? 1 : 0;
    }

}
